/**
 * DMToolsWindow.java
 * 
 * @author	Joe Scharnitzke
 * Date: 	Jun 6, 2011
 * 
 * This class defines the GUI for the DM Tools.
 *
 * Change Log:
 * 	06/16/11 18:40 - (v1.1)
 * 						New Character Interface
 * 						> disposes itself if the user cancels
 * 	06/10/11 10:56 - (v1.0) Release
 * 	Jun 6, 2011 10:35:48 AM - (v0.1) Initial Implementation
 */

package gui;

import java.awt.event.*;
import java.awt.image.*;
import java.awt.*;
import javax.swing.*;
import javax.imageio.*;
import java.io.*;
import java.util.*;

import simpio.*;
import ds.Character;
import rng.*;

public class DMToolsWindow extends JFrame implements ActionListener
{
    private static final long serialVersionUID = -5862576407947792677L;
    
    private CharGen cg_rng = new CharGen();
    
	private JMenuBar jmb_main = new JMenuBar();
		private JMenu jm_chars = new JMenu("Characters");
			private JMenu jm_new = new JMenu("New");
				private JMenuItem jmi_blank = new JMenuItem("Blank");
				private JMenuItem jmi_random = new JMenuItem("Random");
			private JMenuItem jmi_del = new JMenuItem("Delete");
		private JMenu jm_combat = new JMenu("Combat");
			private JMenuItem jmi_enc_gen = new JMenuItem("Ecounters");
			private JMenuItem jmi_init = new JMenuItem("Initiative");
	
	private JPanel jp_dice = new JPanel(new GridBagLayout());
		private JTextField jtf_die_quant1 = new JTextField();
		private JTextField jtf_die_quant2 = new JTextField();
		private JTextField jtf_die_quant3 = new JTextField();
		private JTextField jtf_die_quant4 = new JTextField();
		private JTextField jtf_die1 = new JTextField();
		private JTextField jtf_die2 = new JTextField();
		private JTextField jtf_die3 = new JTextField();
		private JTextField jtf_die4 = new JTextField();
		private JCheckBox jcb_use_die2 = new JCheckBox();
		private JCheckBox jcb_use_die3 = new JCheckBox();
		private JCheckBox jcb_use_die4 = new JCheckBox();
		private JLabel jl_x1 = new JLabel("x");
		private JLabel jl_x2 = new JLabel("x");
		private JLabel jl_x3 = new JLabel("x");
		private JLabel jl_x4 = new JLabel("x");
		private JLabel jl_roll = new JLabel("");
		private JLabel jl_quant = new JLabel("#");
		private JLabel jl_die = new JLabel("Die");
	
	private JPanel jp_output = new JPanel(new GridBagLayout());
		private JTextArea jta_rolls = new JTextArea();	
		private JButton jb_roll = new JButton("Roll");
	
	private GridBagConstraints gbc_main = new GridBagConstraints();
	private GridBagConstraints gbc_dice = new GridBagConstraints();
	private GridBagConstraints gbc_output = new GridBagConstraints();
	
	/* Assets for temporary new character panel */
	private JFrame jf_new_char = new JFrame();
	private CharacterPane cp_new_char;
	
	private JMenuBar jmb_new_char = new JMenuBar();
		private JMenu jm_sheet = new JMenu("Sheet");
			private JMenuItem jmi_save = new JMenuItem("Save");
			private JMenuItem jmi_close = new JMenuItem("Close");
	
	/**
	 * Default constructor for DMToolsWindow objects.
	 */
			
	public DMToolsWindow()
	{
		setLayout(new GridBagLayout());
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		//try to load the default logo
		try
		{
			BufferedImage bi_d20 = ImageIO.read(new File("assets/images/d20icon.png"));
			setIconImage(bi_d20);
		}
		
		catch(Exception e)
		{
			System.err.println(e);
		}
		
		jtf_die_quant1.setHorizontalAlignment(JTextField.CENTER);
		jtf_die_quant2.setHorizontalAlignment(JTextField.CENTER);
		jtf_die_quant3.setHorizontalAlignment(JTextField.CENTER);
		jtf_die_quant4.setHorizontalAlignment(JTextField.CENTER);
		jtf_die1.setHorizontalAlignment(JTextField.CENTER);
		jtf_die2.setHorizontalAlignment(JTextField.CENTER);
		jtf_die3.setHorizontalAlignment(JTextField.CENTER);
		jtf_die4.setHorizontalAlignment(JTextField.CENTER);
		
		jb_roll.addActionListener(this);
		jmi_blank.addActionListener(this);
		jmi_random.addActionListener(this);
		jmi_del.addActionListener(this);
		jmi_enc_gen.addActionListener(this);
		jmi_init.addActionListener(this);
		
		jmi_blank.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		jmi_init.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.CTRL_MASK));
		
		jm_chars.setMnemonic(KeyEvent.VK_C);
		jm_combat.setMnemonic(KeyEvent.VK_M);
		jm_new.setMnemonic(KeyEvent.VK_N);
		jmi_blank.setMnemonic(KeyEvent.VK_B);
		jmi_random.setMnemonic(KeyEvent.VK_R);
		
		jm_new.add(jmi_blank);
		jm_new.add(jmi_random);
		jm_chars.add(jm_new);
		jm_chars.add(jmi_del);
		jmb_main.add(jm_chars);
		
		jm_combat.add(jmi_init);
		jm_combat.add(jmi_enc_gen);
		jmb_main.add(jm_combat);
		
		setJMenuBar(jmb_main);
		
		gbc_dice.anchor = GridBagConstraints.SOUTH;
		
		/* Add text fields to jp_dice */
		
		//add jtf_die_quant1 to jp_dice
		gbc_dice.fill = GridBagConstraints.HORIZONTAL;
		gbc_dice.gridx = 1;
		gbc_dice.gridy = 1;
		gbc_dice.weightx = 1.0;
		gbc_dice.weighty = 1.0;
		gbc_dice.insets = new Insets(2, 2, 2, 2);
		jp_dice.add(jtf_die_quant1, gbc_dice);

		//add jtf_die_quant2 to jp_dice
		gbc_dice.fill = GridBagConstraints.HORIZONTAL;
		gbc_dice.gridx = 1;
		gbc_dice.gridy = 2;
		gbc_dice.weightx = 1.0;
		gbc_dice.weighty = 1.0;
		gbc_dice.insets = new Insets(2, 2, 2, 2);
		jp_dice.add(jtf_die_quant2, gbc_dice);
		
		//add jtf_die_quant3 to jp_dice
		gbc_dice.fill = GridBagConstraints.HORIZONTAL;
		gbc_dice.gridx = 1;
		gbc_dice.gridy = 3;
		gbc_dice.weightx = 1.0;
		gbc_dice.weighty = 1.0;
		gbc_dice.insets = new Insets(2, 2, 2, 2);
		jp_dice.add(jtf_die_quant3, gbc_dice);
		
		//add jtf_die_quant4 to jp_dice
		gbc_dice.fill = GridBagConstraints.HORIZONTAL;
		gbc_dice.gridx = 1;
		gbc_dice.gridy = 4;
		gbc_dice.weightx = 1.0;
		gbc_dice.weighty = 1.0;
		gbc_dice.insets = new Insets(2, 2, 2, 2);
		jp_dice.add(jtf_die_quant4, gbc_dice);
		
		//add jtf_die1 to jp_dice
		gbc_dice.fill = GridBagConstraints.HORIZONTAL;
		gbc_dice.gridx = 3;
		gbc_dice.gridy = 1;
		gbc_dice.weightx = 1.0;
		gbc_dice.weighty = 1.0;
		gbc_dice.insets = new Insets(2, 2, 2, 2);
		jp_dice.add(jtf_die1, gbc_dice);
		
		//add jtf_die2 to jp_dice
		gbc_dice.fill = GridBagConstraints.HORIZONTAL;
		gbc_dice.gridx = 3;
		gbc_dice.gridy = 2;
		gbc_dice.weightx = 1.0;
		gbc_dice.weighty = 1.0;
		gbc_dice.insets = new Insets(2, 2, 2, 2);
		jp_dice.add(jtf_die2, gbc_dice);
		
		//add jtf_die3 to jp_dice
		gbc_dice.fill = GridBagConstraints.HORIZONTAL;
		gbc_dice.gridx = 3;
		gbc_dice.gridy = 3;
		gbc_dice.weightx = 1.0;
		gbc_dice.weighty = 1.0;
		gbc_dice.insets = new Insets(2, 2, 2, 2);
		jp_dice.add(jtf_die3, gbc_dice);
		
		//add jtf_die4 to jp_dice
		gbc_dice.fill = GridBagConstraints.HORIZONTAL;
		gbc_dice.gridx = 3;
		gbc_dice.gridy = 4;
		gbc_dice.weightx = 1.0;
		gbc_dice.weighty = 1.0;
		gbc_dice.insets = new Insets(2, 2, 2, 2);
		jp_dice.add(jtf_die4, gbc_dice);
		
		/* Add check boxes to jp_dice */
		
		//add jcb_use_die2 to jp_dice
		gbc_dice.fill = GridBagConstraints.NONE;
		gbc_dice.gridx = 0;
		gbc_dice.gridy = 2;
		gbc_dice.weightx = 0.0;
		gbc_dice.weighty = 0.0;
		gbc_dice.insets = new Insets(2, 2, 2, 2);
		jp_dice.add(jcb_use_die2, gbc_dice);
		
		//add jcb_use_die3 to jp_dice
		gbc_dice.fill = GridBagConstraints.NONE;
		gbc_dice.gridx = 0;
		gbc_dice.gridy = 3;
		gbc_dice.weightx = 0.0;
		gbc_dice.weighty = 0.0;
		gbc_dice.insets = new Insets(2, 2, 2, 2);
		jp_dice.add(jcb_use_die3, gbc_dice);
		
		//add jcb_use_die4 to jp_dice
		gbc_dice.fill = GridBagConstraints.NONE;
		gbc_dice.gridx = 0;
		gbc_dice.gridy = 4;
		gbc_dice.weightx = 0.0;
		gbc_dice.weighty = 0.0;
		gbc_dice.insets = new Insets(2, 2, 2, 2);
		jp_dice.add(jcb_use_die4, gbc_dice);
		
		/* Add labels to jp_dice */
		
		gbc_dice.anchor = GridBagConstraints.SOUTH;
		
		//add jl_roll
		gbc_dice.fill = GridBagConstraints.HORIZONTAL;
		gbc_dice.gridx = 0;
		gbc_dice.gridy = 0;
		gbc_dice.weightx = 1.0;
		gbc_dice.weighty = 1.0;
		gbc_dice.gridwidth = 1;
		gbc_dice.gridheight = 1;
		gbc_dice.insets = new Insets(0, 7, 0, 7);
		jp_dice.add(jl_roll, gbc_dice);
		
		//add jl_quant
		gbc_dice.fill = GridBagConstraints.HORIZONTAL;
		gbc_dice.gridx = 1;
		gbc_dice.gridy = 0;
		gbc_dice.weightx = 1.0;
		gbc_dice.weighty = 1.0;
		gbc_dice.gridwidth = 1;
		gbc_dice.gridheight = 1;
		gbc_dice.insets = new Insets(0, 12, 0, 12);
		jp_dice.add(jl_quant, gbc_dice);
		
		//add jl_die
		gbc_dice.fill = GridBagConstraints.HORIZONTAL;
		gbc_dice.gridx = 3;
		gbc_dice.gridy = 0;
		gbc_dice.weightx = 1.0;
		gbc_dice.weighty = 1.0;
		gbc_dice.gridwidth = 1;
		gbc_dice.gridheight = 1;
		gbc_dice.insets = new Insets(0, 6, 0, 6);
		jp_dice.add(jl_die, gbc_dice);
		
		//add jl_x1
		gbc_dice.fill = GridBagConstraints.NONE;
		gbc_dice.gridx = 2;
		gbc_dice.gridy = 1;
		gbc_dice.weightx = 0.0;
		gbc_dice.weighty = 1.0;
		gbc_dice.insets = new Insets(2, 2, 2, 2);
		jp_dice.add(jl_x1, gbc_dice);
		
		//add jl_x2
		gbc_dice.fill = GridBagConstraints.NONE;
		gbc_dice.gridx = 2;
		gbc_dice.gridy = 2;
		gbc_dice.weightx = 0.0;
		gbc_dice.weighty = 0.0;
		gbc_dice.insets = new Insets(2, 2, 2, 2);
		jp_dice.add(jl_x2, gbc_dice);
		
		//add jl_x3
		gbc_dice.fill = GridBagConstraints.NONE;
		gbc_dice.gridx = 2;
		gbc_dice.gridy = 3;
		gbc_dice.weightx = 0.0;
		gbc_dice.weighty = 0.0;
		gbc_dice.insets = new Insets(2, 2, 2, 2);
		jp_dice.add(jl_x3, gbc_dice);
		
		//add jl_x4
		gbc_dice.fill = GridBagConstraints.NONE;
		gbc_dice.gridx = 2;
		gbc_dice.gridy = 4;
		gbc_dice.weightx = 0.0;
		gbc_dice.weighty = 0.0;
		gbc_dice.insets = new Insets(2, 2, 2, 2);
		jp_dice.add(jl_x4, gbc_dice);
		
		
		/* Add text areas to jp_output */
		
		//add jta_rolls to jp_output
		jta_rolls.setBorder(BorderFactory.createLoweredBevelBorder());
		jta_rolls.setWrapStyleWord(true);
		jta_rolls.setLineWrap(true);
		jta_rolls.setEditable(false);
		jta_rolls.setFocusable(false);
		
		gbc_output.fill = GridBagConstraints.BOTH;
		gbc_output.gridx = 0;
		gbc_output.gridy = 0;
		gbc_output.weightx = 1.0;
		gbc_output.weighty = 1.0;
		gbc_output.gridwidth = 1;
		gbc_output.gridheight = 1;
		gbc_output.anchor = GridBagConstraints.NORTHEAST;
		gbc_output.insets = new Insets(5, 5, 5, 5);
		jp_output.add(jta_rolls, gbc_output);
		
		/* Add buttons to jp_output */
		
		//add jb_roll to jp_output
		gbc_output.fill = GridBagConstraints.NONE;
		gbc_output.gridx = 0;
		gbc_output.gridy = 1;
		gbc_output.weightx = 1.0;
		gbc_output.weighty = 0.0;
		gbc_output.gridwidth = 2;
		gbc_output.gridheight = 1;
		gbc_output.insets = new Insets(0, 5, 5, 5);
		gbc_output.anchor = GridBagConstraints.SOUTHEAST;
		jp_output.add(jb_roll, gbc_output);		
		
		/* Add panels to this */
		
		gbc_main.fill = GridBagConstraints.NONE;
		gbc_main.gridx = 0;
		gbc_main.gridy = 0;
		gbc_main.weightx = 0.0;
		gbc_main.weighty = 0.0;
		gbc_main.gridwidth = 1;
		gbc_main.gridheight = 1;
		gbc_main.insets = new Insets(5, 5, 5, 5);
		gbc_main.anchor = GridBagConstraints.WEST;
		add(jp_dice, gbc_main);
		
		gbc_main.fill = GridBagConstraints.BOTH;
		gbc_main.gridx = 1;
		gbc_main.gridy = 0;
		gbc_main.weightx = 1.0;
		gbc_main.weighty = 1.0;
		gbc_main.gridwidth = 1;
		gbc_main.gridheight = 1;
		gbc_main.insets = new Insets(5, 5, 5, 5);
		gbc_main.anchor = GridBagConstraints.NORTH;
		add(jp_output, gbc_main);
		
		setTitle("DM Tools");
		setSize(550, 250);
		setResizable(false);
		GUIHelper.Center(this);
		setVisible(true);
		
		jtf_die_quant1.requestFocusInWindow();
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent event)
	{
		//user clicks Roll button
		if(event.getSource().equals(jb_roll))
		{
			String s_output = "";
			
			try
			{
				int i_quant = Integer.parseInt(jtf_die_quant1.getText());
				int i_die = Integer.parseInt(jtf_die1.getText());
				
				s_output += i_quant + "d" + i_die + ":  ";
				
				for(int i = 0; i < i_quant; i++)
				{
					s_output += (int)(Math.random() * i_die + 1) + "  ";
				}
				
				s_output += "\n\n";
				
				//only use other dice if they're selected
				if(jcb_use_die2.isSelected())
				{
					i_quant = Integer.parseInt((jtf_die_quant2.getText()));
					i_die = Integer.parseInt(jtf_die2.getText());
					
					s_output += i_quant + "d" + i_die + ":  ";
					
					for(int i = 0; i < i_quant; i++)
					{
						s_output += (int)(Math.random() * i_die + 1) + "  ";
					}
					
					s_output += "\n\n";
				}
				
				if(jcb_use_die3.isSelected())
				{
					i_quant = Integer.parseInt((jtf_die_quant3.getText()));
					i_die = Integer.parseInt(jtf_die3.getText());
					
					s_output += i_quant + "d" + i_die + ":  ";
					
					for(int i = 0; i < i_quant; i++)
					{
						s_output += (int)(Math.random() * i_die + 1) + "  ";
					}
					
					s_output += "\n\n";
				}
				
				if(jcb_use_die4.isSelected())
				{
					i_quant = Integer.parseInt((jtf_die_quant4.getText()));
					i_die = Integer.parseInt(jtf_die4.getText());
					
					s_output += i_quant + "d" + i_die + ":  ";
					
					for(int i = 0; i < i_quant; i++)
					{
						s_output += (int)(Math.random() * i_die + 1) + "  ";
					}
					
					s_output += "\n\n";
				}
				
				jta_rolls.setText(s_output);
				jta_rolls.revalidate();
			}
			
			catch(Exception e)
			{
				jtf_die_quant1.setText("");
				jtf_die_quant2.setText("");
				jtf_die_quant3.setText("");
				jtf_die_quant4.setText("");
				jtf_die1.setText("");
				jtf_die2.setText("");
				jtf_die3.setText("");
				jtf_die4.setText("");
				
				jtf_die_quant1.requestFocusInWindow();
			}
		}
		
		//user creates a new blank character
		if(event.getSource().equals(jmi_blank))
		{
			setEnabled(false);
			
			try
			{
				
				String[] sa_new_char = CharCreateWindow.newChar();
								
				if(sa_new_char != null)
				{
					Character ch_new_char = new Character(sa_new_char[0], sa_new_char[1], sa_new_char[2], sa_new_char[3]);
					cp_new_char = new CharacterPane(ch_new_char);
							
					jf_new_char.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
					jf_new_char.setTitle(sa_new_char[0]);
					
					//try to load the default logo
					try
					{
						BufferedImage bi_d20 = ImageIO.read(new File("assets/images/d20icon.png"));
						jf_new_char.setIconImage(bi_d20);
					}
					
					catch(Exception e)
					{
						System.err.println(e);
					}
							
					jmi_save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
					jmi_close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
					
					jmi_save.addActionListener(this);
					jmi_close.addActionListener(this);
					
					jm_sheet.add(jmi_save);
					jm_sheet.add(jmi_close);
					
					jmb_new_char.add(jm_sheet);
					
					jf_new_char.setJMenuBar(jmb_new_char);
					jf_new_char.add(cp_new_char);
					jf_new_char.setSize(500, 400);
					GUIHelper.Center(jf_new_char);
					jf_new_char.setVisible(true);
				}
			}
			
			catch(Exception e)
			{
				System.err.println(e);
			}
		}
		
		//user creates a new random character
		if(event.getSource().equals(jmi_random))
		{
			try
			{
				String s_name, s_class, s_race, s_player;
				int[] ia_stats = cg_rng.statGenMinMod(6, 4);
				
				s_name = JOptionPane.showInputDialog(null, "Character Name");
				s_class = JOptionPane.showInputDialog(null, "Character Class");
				s_race = JOptionPane.showInputDialog(null, "Character Race");
				s_player = JOptionPane.showInputDialog(null, "Player Name:");
				
				Character ch_new_char = new Character(s_name, s_class, s_race, s_player);
				
				ch_new_char.setIStr(ia_stats[0]);
				ch_new_char.setIAgi(ia_stats[1]);
				ch_new_char.setIInt(ia_stats[2]);
				ch_new_char.setIFoc(ia_stats[3]);
				ch_new_char.setICon(ia_stats[4]);
				ch_new_char.setICha(ia_stats[5]);
				
				cp_new_char = new CharacterPane(ch_new_char);
						
				jf_new_char.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
				jf_new_char.setTitle("New Character");
				
				//try to load the default logo
				try
				{
					BufferedImage bi_d20 = ImageIO.read(new File("assets/images/d20icon.png"));
					jf_new_char.setIconImage(bi_d20);
				}
				
				catch(Exception e)
				{
					System.err.println(e);
				}
						
				jmi_save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
				jmi_close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
				
				jmi_save.addActionListener(this);
				jmi_close.addActionListener(this);
				
				jm_sheet.add(jmi_save);
				jm_sheet.add(jmi_close);
				
				jmb_new_char.add(jm_sheet);
				
				jf_new_char.setJMenuBar(jmb_new_char);
				jf_new_char.add(cp_new_char);
				jf_new_char.setSize(500, 400);
				GUIHelper.Center(jf_new_char);
				jf_new_char.setVisible(true);
			}
			
			catch(Exception e)
			{
				System.err.println(e);
			}
		}
		
		if(event.getSource().equals(jmi_save))
		{
			CharParser cp_saver = new CharParser(cp_new_char.getChar().getSName());
			cp_saver.saveChar(cp_new_char.getChar());	
		}
		
		if(event.getSource().equals(jmi_close))
		{
			jf_new_char.dispose();
		}
		
		if(event.getSource().equals(jmi_del))
		{
			CharParser cp_file_list = new CharParser();
			Vector<String> sv_files = cp_file_list.getSaved();
			new CharDelWindow(sv_files);
		}
	}
}
